/*
 * @Description: 光盘检测 - 安全性检测
 * @Author: Rfan
 * @Date: 2022-10-10 14:19:09
 * @LastEditTime: 2022-10-21 18:11:18
 */

import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { Checkbox, Space } from 'antd';
import cs from 'classnames';
import classnames from 'classnames/bind';
import { useAddPlanDialogContext } from '../../../contexts/AddPlanDialogContext';
import { CheckProps, EnumSecurityRuleType } from '../../../../../../dictionaries';
import type { IPropsRule } from '../../../../../../interface';
import styles from '../../../styles/entryPanes.module.scss';
import mainStyles from '../../../styles/main.module.scss';
import { isNullObj } from '@/utils';

const cx = classnames.bind(styles);
const mainCx = classnames.bind(mainStyles);

// CD安全性规则列表类型
type TRuleList = IPropsRule<EnumSecurityRuleType>[];

// CD安全性完整类型
interface ISecurityFullRules {
  ruleType: CheckProps.Security;
  rules: TRuleList;
}

const CDSecurityCheck = (props: any, ref: any) => {
  const { currentRuleInfo, getPropertyRules, getRuleState, setRuleState } =
    useAddPlanDialogContext();

  // 规则列表
  const [ruleList, setRuleList] = useState<TRuleList>([]);

  useImperativeHandle(ref, () => ({
    save: () => {
      const fullRules: ISecurityFullRules = {
        ruleType: CheckProps.Security,
        rules: ruleList
      };
      return fullRules;
    }
  }));

  /**
   * @description: 获取到规则后回写
   */
  useEffect(() => {
    if (currentRuleInfo && !isNullObj(currentRuleInfo)) {
      const ruleObj = getPropertyRules(CheckProps.Security);
      setRuleList(ruleObj?.rules || []);
    }
  }, [currentRuleInfo]);

  return (
    <div className={cs(mainCx('property-wrap'), cx('property-security'))}>
      <Space className={mainCx('property--title-wrap')} align="center">
        <h6 className={mainCx('property--title')}>光盘格式包</h6>
      </Space>
      <Space direction="vertical">
        <Checkbox
          checked={getRuleState(EnumSecurityRuleType.DiskExtraFiles, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumSecurityRuleType.DiskExtraFiles,
              ruleList,
              setRuleList
            );
          }}
        >
          载体中多余文件检测（对载体进行读取操作，判断载体内是否含有非归档文件）
        </Checkbox>
        <Checkbox
          checked={getRuleState(EnumSecurityRuleType.DiskReadSpeed, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumSecurityRuleType.DiskReadSpeed,
              ruleList,
              setRuleList
            );
          }}
        >
          载体读取速度检测（对载体进行读取操作，和常规的读取速度进行比对判断载体是否安全可靠）
        </Checkbox>
        <Checkbox
          checked={getRuleState(EnumSecurityRuleType.DiskConformity, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumSecurityRuleType.DiskConformity,
              ruleList,
              setRuleList
            );
          }}
        >
          光盘合格性检测（对光盘的PIE、POF、BLER等指标进行检测，判断光盘是否合格）
        </Checkbox>
      </Space>
    </div>
  );
};

export default forwardRef(CDSecurityCheck);
